//Class that represent a map

Map = function(){
	
	//difference between canvas and map coods
	var mapX;
	var mapY;
	
	var width;
	var height;
	var cell;//Array of array
	
	var ctx;
	
	//getter And Setter
	Map.prototype.setMapX = function(aMapX){
			mapX = aMapX;
	}
	Map.prototype.getMapX = function(){
			return mapX;
	}
	Map.prototype.setMapY = function(aMapY){
			mapY = aMapY;
	}
	Map.prototype.getMapY = function(){
			return mapY;
	}
	
	Map.prototype.setWidth = function(aWidth){
			width = aWidth;
	}
	Map.prototype.getWidth = function(){
			return width;
	}
	Map.prototype.setHeight = function(aHeight){
			height = aHeight;
	}
	Map.prototype.getHeight = function(){
			return height;
	}
	
	Map.prototype.initCell = function(){
			//TO DO remove this HACK -> cell =  [[][][]]...
			cell=new Array();
			cell[0]= new Array();
			cell[1]= new Array();
	}
	Map.prototype.setCell = function(i,j,aCell){
			cell[i][j]=aCell;
	}
	Map.prototype.getCell = function(){
			return cell;
	}
	
	Map.prototype.setCtx = function(aCtx){
			ctx = aCtx;
	}
	Map.prototype.getCtx = function(){
			return ctx;
	}
}

Map.prototype.initialize = function(res, can){
	console.log("Map start initializing.");
	//TO DO modify next line...
	this.initCell();
	
	this.setCtx(can.getContext("2d"));
	
	var mapArray = res.getMapArray();
	this.setWidth(mapArray.length);
	this.setHeight(mapArray[0].length);
	
	//TO DO fix cste to tileUnit in config..
	var tileUnit = 50;
	this.setMapX(40+this.getHeight()*tileUnit*1.17);//tileUnit*1.17 is some kind of f(tileUnit,sin15,sin75,sin45,sin13.5) that I didn't calculate
	this.setMapY(40);
	
	//TO DO put this cste in config file
	var sin45 = 0.707106781; //Pi/4
	var sin15 = 0.258819045; //Pi/12
	var sin13_5 = 0.233445364; //???->I used Approximation
	var sin75 = 0.965925826; //5*Pi/12
	
	for (var i=0; i<this.getWidth(); i++) {
		for (var j=0; j<this.getHeight(); j++) {
			//OLD version not working cause all cell same reference
			var cell = new Cell();
			var diff = (j+Math.floor((i+1)/2));
			var x = (i*(sin45+sin75)-diff*(sin15+sin75))*tileUnit+this.getMapX();
			var y = ((diff*(sin15+sin75)/2)+i*sin13_5)*tileUnit+this.getMapY();
			cell.initialize(this.getCtx(),mapArray[i][j],[x,y]);
			this.setCell(i,j,cell);
			//this.setCell(i,j,new Cell());
			//this.getCell()[i][j].initialize(this.getCtx(),mapArray[i][j],[(i*(sin45+sin75)-(j+Math.floor((i+1)/2))*(sin15+sin75))*tileUnit+this.getMapX(),(((j+Math.floor((i+1)/2))*(sin15+sin75)/2)+i*sin13_5)*tileUnit+this.getMapY()]);
		}
	}
	//TO DO put Tile.prototype.getImgArray = res.getTileImg() ...
	DrawedObject.prototype.getAllImgArray = function(){
			return [res.getTileImg(),res.getOverTileImg(),res.getMonsterImg(),res.getBuildingImg()];
	}
	res.setMapArray(null);//To free memory
	console.log("Map finish initializing.");
}

Map.prototype.update = function(){
	console.log("map is Updated");
	//TO DO fix it to canvas.width and canvas.height...
	this.getCtx().clearRect(0,0,1366,768);
	var cells = this.getCell();
	console.log(cells[0][0] == cells[1][1]);
	for (var i=0; i<this.getWidth(); i++) {
		for (var j=0; j<this.getHeight(); j++) {
			cells[i][j].draw();
		}
	}
}
